﻿function Author(p) {
    var self = this;
    this.parent = p;
    this.locked = ko.observable(false);
    this.name = ko.observable().extend({ required: "Please enter login name" });
    this.displayName = ko.observable().extend({ required: "Please enter display name" });
    this.email = ko.observable().extend({ required: "Please enter email" });
    this.primary = ko.observable(false);
    this.enabled = ko.observable(true);
    this.url = ko.observable();

    this.isOdd = ko.observable(true);
    this.isEdit = ko.observable(false);
    this.isNew = ko.observable(false);

    this.oldUrl = ko.observable();
    this.oldName = ko.observable();
    this.oldDisplayName = ko.observable();
    this.oldEmail = ko.observable();
   
    this.load = function (item) {
        this.locked(item.Locked);
        this.name(item.ID);
        this.displayName(item.Name);
        this.email(item.Email);
        this.primary(item.IsPrimary);
        this.enabled(item.Enabled);
        this.url(item.Url);
    }

    this.editEnabled = ko.computed(function () {
        return (this.name() == userId) || this.parent.admin();
    }, this);

    this.edit = function () {
        this.isEdit(true);
        this.oldName(this.name());
        this.oldDisplayName(this.displayName());
        this.oldEmail(this.email());
        this.oldUrl(this.url());
    }

    this.hasError = ko.computed(function () {
        return this.name.hasError() || this.email.hasError() || this.displayName.hasError();
    }, this);

    this.setError = function () {
        if (this.hasError()) {
            this.parent.errorMessage("Please correct the following errors:");
            if (this.name.hasError())
                this.parent.errors.push(this.name.validationMessage());
            if (this.displayName.hasError())
                this.parent.errors.push(this.displayName.validationMessage());
            if (this.email.hasError())
                this.parent.errors.push(this.email.validationMessage());
        } else {
            this.clearError();
        }
    }

    this.clearError = function () {
        this.parent.errors.removeAll();
    }

    this.update = function () {
        if (this.hasError()) {
            this.setError();
        } else {
            var url, input;
            if (this.isEdit() && !this.isNew()) {
                url = appPath + 'Author/Update/'
                input = {
                    olid: this.oldName(),
                    ID: this.name(),
                    Name: this.displayName(),
                    Email: this.email(),
                    Url: this.url(),
                    IsPrimary: this.primary(),
                    Enabled: this.enabled()
                };
            } else {
                url = appPath + 'Author/Create/'
                input = {
                    ID: this.name(),
                    Name: this.displayName(),
                    Email: this.email(),
                    Url: this.url()
                };
            }
           
            $.post(url, input, function (data) {
                if (data.Result) {
                    self.parent.redoOdd();
                    self.parent.successMessage(data.Message);
                    self.clearError();
                    setInterval(function () { self.parent.successMessage('') }, settings.fadeOutInterval);
                    self.isEdit(false);
                    if (self.isNew()) {
                        self.isNew(false);
                        self.parent.addEnable(true);
                    }
                } else {
                    self.parent.errorMessage(data.Message);
                    if (data.Errors) {
                        for (var idx = 0; idx < data.Errors.length; idx++)
                            self.parent.errors.push(data.Errors[idx]);
                    }
                }
            });
        }
    }

    this.remove = function () {
        var url = appPath + 'Author/Delete/' + this.name();
        $.get(url, function (data) {
            if (data.Result) {
                self.parent.successMessage(data.Message);
                setInterval(function () { self.parent.successMessage('') }, settings.fadeOutInterval);
                self.clearError();
                self.parent.remove(self);
                self.parent.redoOdd();
            } else {
                self.parent.errorMessage(data.Message);
            }
        });
    }

    this.unlockEnabled = ko.computed(function () {
        return admin && this.locked();
    }, this);

    this.unlock = function () {
        var url = appPath + 'Author/Unlock/' + this.name();
        $.get(url, function (data) {
            if (data.Result) {
                self.parent.successMessage(data.Message);
                setTimeout(function () { self.parent.successMessage('') }, settings.fadeOutInterval);
                self.locked(false);
                self.clearError();
            } else {
                self.parent.errorMessage(data.Message);
            }
        });
    }

    this.disableEnabled = ko.computed(function () {
        return this.parent.admin() && !this.primary();
    }, this);

    this.disableText = ko.computed(function () {
        return this.enabled() ? 'Disable' : 'Enable';
    }, this);

    this.disable = function () {
        var url = appPath + 'Author/Disable/' + this.name() + "?enable=" + !this.enabled();
        $.get(url, function (data) {
            if (data.Result) {
                self.parent.successMessage(data.Message);
                setTimeout(function () { self.parent.successMessage('') }, settings.fadeOutInterval);
                self.enabled(!self.enabled());
                self.clearError();
            } else {
                self.parent.errorMessage(data.Message);
            }
        });
    }

    this.primaryEnabled = ko.computed(function () {
        return this.parent.admin() && !this.primary();
    }, this);

    this.makePrimary = function () {
        var url = appPath + 'Author/MakePrimary/' + this.name();
        $.get(url, function (data) {
            if (data.Result) {
                self.parent.successMessage(data.Message);
                setTimeout(function () { self.parent.successMessage('') }, settings.fadeOutInterval);
                self.parent.makePrimary(self.name());
                self.parent.admin(false);
                self.clearError();
            } else {
                self.parent.errorMessage(data.Message);
            }
        });
    }

    this.close = function () {
        this.clearError();
        if (this.isEdit() && !this.isNew()) {
            this.isEdit(false);
            this.name(this.oldName());
            this.displayName(this.oldDisplayName());
            this.email(this.oldEmail());
            this.url(this.oldUrl());
        }
        
        if (this.isNew()) {
            this.parent.collection.pop();
            this.parent.addEnable(true);
        }
    }
}


